package com.echo.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Typeface;

import com.echo.SportsApplication;

import java.util.Locale;

/**
 * 应用相关工具类
 * Created by EchoGe on 16/4/5.
 */
public class AppUtils {


    public static Typeface typeFaceCN = Typeface.createFromAsset(SportsApplication.getInstance().getAssets(), "fonts/Normal.otf");

    private AppUtils()
    {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");

    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context)
    {
        try
        {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static Typeface getTypeFaceCN() {
        return typeFaceCN;
    }

    public static Locale getAppLocale() {
        try {
            Configuration config = SportsApplication.getInstance().getResources()
                    .getConfiguration();
            if (config != null) {
                return config.locale;
            }
        } catch (Exception e) {
        }
        return Locale.getDefault();
    }
}
